package dtaskboard.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import dtaskboard.client.dto.HistoryDTO;
import dtaskboard.client.dto.TaskDTO;

@Entity
public class History implements Comparable<History> {

  private Long id;
  private String name;
  private Long hours;
  private Collection<Task> tasks;


  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Long getHours() {
    return this.hours;
  }

  public void setHours(Long hours) {
    this.hours = hours;
  }

  @OneToMany
  public Collection<Task> getTasks() {
    if (this.tasks == null) {
      this.tasks = new HashSet<Task>();
    }
    return this.tasks;
  }

  public void setTasks(Collection<Task> tasks) {
    this.tasks = tasks;
  }

  @Override
  public int compareTo(History o) {
    return this.name.compareTo(o.getName());
  }

  public HistoryDTO toDTO() {
    HistoryDTO dto = new HistoryDTO();
    dto.setHistoryId(this.id);
    dto.setName(this.name);
    Collection<TaskDTO> tasks = new ArrayList<TaskDTO>();
    for (Task t : this.getTasks()) {
      tasks.add(t.toDTO());
    }
    dto.setTasks(tasks);
    return dto;
  }

}
